import 'dart:convert';
import 'package:flutter_mark_trip/model/travel_model.dart';
import 'package:http/http.dart' as http;

///*入参
//var params = {
//  "districtId": -1,
//  "groupChannelCode": "tourphoto_global1",
//  "type": 1,
//  "lat": -180,
//  "lon": -180,
//  "locatedDistrictId": 0,
//  "pagePara": {
//    "pageIndex": 1,
//    "pageSize": 10,
//    "sortType": 9,
//    "sortDirection": 0
//  },
//  "imageCutType": 1,
//  "head": {
//    "cid": "09031043111839013741",
//    "ctok": "",
//    "cver": "1.0",
//    "lang": "01",
//    "sid": "8888",
//    "syscode": "09",
//    "auth": null,
//    "extension": [
//      {"name": "protocal", "value": "https"}
//    ]
//  },
//  "contentType": "json"
//};

///旅拍页dao层 接口
class TravelDao {
  ///旅拍页dao层
  static Future<TravelModel> fetch(
      String url,Map params, String groupChannelCode, int pageIndex, int pageSize) async {
    Map paramMap = params['pagePara'];
    paramMap['pageIndex'] = pageIndex;
    paramMap['pageSize'] = pageSize;
    params['groupChannelCode'] = groupChannelCode;

    print(url+"body:"+jsonEncode(params));
    final response = await http.post(
      url,
      body: jsonEncode(params),
    );
    if (response.statusCode == 200) {
      Utf8Decoder utf8decoder = Utf8Decoder(); //fix 中文乱码
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      print(result);
      return TravelModel.fromJson(result);
    } else {
      //抛出异常，加载接口数据失败时
      throw Exception('Failed to load travel');
    }
  }
}
